import http from '@/utils/http.js'
import * as types from '../actionType'

export const setChannels = (payload) => ({ type: types.SET_CHANNELS, payload })
export const setArticles = (payload) => ({ type: types.SET_ARTICLES, payload })

export const getChannels = () => {
    return async (dispatch) => {
        const { data: { channels } } = await http.get('/v1_0/channels')
        dispatch(setChannels(channels))
    }
}

export const getArticles = (params) => {
    return async (dispatch) => {
        const { data } = await http.get('/v1_0/mp/articles', { params })
        dispatch(setArticles(data))
    }
}

export const delArticle = (id) => {
    return async dispatch => {
        await http.delete(`/v1_0/mp/articles/${id}`)
    }
}

export const addArticle = (data, draft = false) => {
    return async dispatch => {
        await http.post('/v1_0/mp/articles?draft=' + draft, data)
    }
}

export const editArticle = (data, draft = false) => {
    return async dispatch => {
        await http.put(`/v1_0/mp/articles/${data.id}?draft=${draft}`, data)
    }
}

export const getArticle = id => {
    return async dispatch => {
        const data = await http.get('/v1_0/mp/articles/' + id)
        return data
    }
}